import { defineStore } from "pinia";

//选项式写法 有this
export const useCounterStore = defineStore("counter", {
  state: () => ({
    count: 0,
  }),
  actions: {
    increment(amount: number) {
      console.log("increment");
      this.count += amount;
    },
    decrement(amount: number) {
      console.log("decrement");
      this.count -= amount;
    },
  },
  getters: {
    doubleCount(): number {
      return this.count * 2;
    },
    tripleCount: (state) => state.count * 3,
  },
});

//组合式api写法 无this
import { computed, ref } from "vue";
export const useCounterStore2 = defineStore("counter2", () => {
  let count = ref(0);

  const increment = (amount: number) => {
    console.log("increment");
    count.value += amount;
  };
  const decrement = (amount: number) => {
    console.log("decrement");
    count.value -= amount;
  };
  const doubleCount = computed(() => count.value * 2);
  const tripleCount = computed(() => count.value * 3);

  return {
    count,    
    increment,
    decrement,
    doubleCount,
    tripleCount,  
  }
});
